﻿//------------ Custom ----------

var imageCropWidth = 0;
var imageCropHeight = 0;
var cropPointX = 0;
var cropPointY = 0;

$(function () {
    //'use strict';
    $('#fileupload').fileupload({
        url: '/ajax/Upload/UploadImage',
        acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
        dataType: 'text',
        progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            $('#progress .progress-bar').css(
                'width',
                progress + '%'
            );
        },
        done: function (e, data) {
            //$.each(data.result.files, function (index, file) {
            //    $('<p/>').text(file.name).appendTo('#files');
            //});
            $('#filesUploaded').html('<img id="avatarDrop"  src="http://s1.img.yan.vn/Top30Image/' + data.result + '" />');
            $('#ImagePath').val(data.result);
        }
    }).prop('disabled', !$.support.fileInput)
        .parent().addClass($.support.fileInput ? undefined : 'disabled');

    $('#filePostContent').fileupload({
        url: '/ajax/Upload/UploadImage',
        acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
        //maxFileSize: 5000000,
        dataType: 'text',
        progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            $('progressPost .progress-bar').css(
                'width',
                progress + '%'
            );
        },
        done: function (e, data) {
            var img = '<img class="img-content"  src="http://s1.img.yan.vn/Top30Image/' + data.result + '" />';
            InsertHTML(img);
        }
    }).prop('disabled', !$.support.fileInput)
        .parent().addClass($.support.fileInput ? undefined : 'disabled');
});

function startDrop() {
    var jcrop_api;
    $(".jcrop-holder").remove();
    jcrop_api = $.Jcrop('#avatarDrop', {
        onSelect: updateCoords,
        aspectRatio: 1 / 1,
        minSize: [300, 300],
        onChange: updateCoords
    }).animateTo([0, 0, 300, 300]);
}
function saveDrop() {

    if (imageCropWidth == 0 && imageCropHeight == 0) {
        alert("Vui lòng chọn vùng ảnh cần drop.");
        return;
    }

    $.ajax({
        url: '/ajax/Upload/SaveThumb',
        type: 'POST',
        data: {
            imagePath: $("#ImagePath").val(),
            cropPointX: cropPointX,
            cropPointY: cropPointY,
            imageCropWidth: imageCropWidth,
            imageCropHeight: imageCropHeight,

        },
        success: function (data) {

        },
        error: function (data) { }
    });
}
function updateCoords(c) {
    imageCropWidth = c.w;
    imageCropHeight = c.h;

    cropPointX = c.x;
    cropPointY = c.y;
};